/**
 * 产成品信息
 */
Ext.define("YX.sbas.Product", {
  extend: "Ext.panel.Panel",
  layout: {
    type: "hbox",
    align: "stretch",
  },
  gridProd: null,
  pelProdList: null,
  gridAllPath: null,
  gridProdPath: null,
  gridOpers: null,
  txtFilterPath: null,
  pelAllPath: null,
  btnProdPathUp: null,
  btnProdPathDown: null,
  pelPath: null,
  tabProd: null,
  txtSearchProdCode: null,
  frmSearch: null,
  txtProdCode: null,
  txtProdVer: null,
  txtProdDesc: null,
  txtProdSpec: null,
  cmbProdType: null,
  numProdQty: null,
  cmbProdUnit: null,
  btnSave: null,
  btnDelete: null,
  btnSearch: null,
  btnReset: null,
  frmOperate: null,
  pelRight: null,
  initComponent: function () {
    var me = this;
    //成品清单表格
    this.gridProd = Ext.create("Ext.grid.Panel", {
      flex: 1,
      columns: [
        {
          text: T.label.prodCode,
          dataIndex: "matCode",
        },
        {
          text: T.label.ver,
          dataIndex: "matVer",
        },
        {
          text: T.label.prodDesc,
          dataIndex: "matDesc",
          width: 200,
        },
        {
          text: T.label.spec,
          dataIndex: "matSpec",
        },
        {
          text: T.label.type,
          dataIndex: "matType",
          renderer: function (value) {
            var typeStore = me.cmbProdType.getStore();
            if (typeStore) {
              var index = typeStore.findExact("key1", value);
              if (index >= 0) {
                return typeStore.getAt(index).get("data1");
              }
            }

            return value;
          },
        },
        {
          xtype: "numbercolumn",
          text: T.label.qty,
          dataIndex: "qty",
        },
        {
          text: T.label.unit,
          dataIndex: "unit",
        },
      ],
      store: Ext.create("Ext.data.JsonStore", {
        fields: [
          "matCode",
          "matVer",
          "matDesc",
          "matSpec",
          "matType",
          {
            type: "number",
            name: "qty",
          },
          "unit",
        ],
        proxy: {
          type: "zbusrpc",
          module: "SbasViewProductService",
          reader: {
            root: "productList",
          },
        },
      }),
      listeners: {
        scope: this,
        select: this.onGridProdSelect,
      },
    });
    this.pelProdList = Ext.create("Ext.panel.Panel", {
      title: T.label.product,
      flex: 1,
      layout: {
        type: "vbox",
        align: "stretch",
      },
      items: [this.gridProd],
      bbar: {
        xtype: "pagingtoolbar",
        displayInfo: true,
        store: this.gridProd.getStore(),
      },
    });

    //产品工艺路线
    this.txtFilterPath = Ext.create("Ext.form.field.Text", {
      fieldLabel: T.label.processPath,
      listeners: {
        scope: this,
        specialkey: this.onTxtFilterPathSpecialKey,
      },
    });
    this.gridAllPath = Ext.create("Ext.grid.Panel", {
      flex: 1,
      viewConfig: {
        getRowClass: function (record, rowIndex, rowParams, store) {
          if (record.get("pathStatus") == "D") {
            return "red-row";
          }
        },
      },
      columns: [
        {
          text: T.label.pathCode,
          dataIndex: "pathCode",
        },
        {
          text: T.label.pathDesc,
          dataIndex: "pathDesc",
          width: 200,
        },
      ],
      store: Ext.create("Ext.data.JsonStore", {
        fields: ["pathCode", "pathDesc", "pathStatus"],
        proxy: {
          type: "zbusrpc",
          module: "SmfrViewProcessPathService",
          reader: {
            root: "pathList",
          },
        },
      }),
      listeners: {
        scope: this,
        itemdblclick: this.onGridAllPathItemDblClick,
        itemclick: this.onGridAllPathItemClick,
      },
    });
    this.pelAllPath = Ext.create("Ext.panel.Panel", {
      title: T.label.allProcessPath,
      flex: 1,
      layout: {
        type: "vbox",
        align: "stretch",
      },
      items: [this.txtFilterPath, this.gridAllPath],
      bbar: {
        xtype: "pagingtoolbar",
        store: this.gridAllPath.getStore(),
      },
    });

    //产品的工艺路线
    this.btnProdPathUp = Ext.create("Ext.button.Button", {
      iconCls: "icon-go-up",
      scope: this,
      handler: this.onBtnProdPathUpClicked,
    });
    this.btnProdPathDown = Ext.create("Ext.button.Button", {
      iconCls: "icon-go-down",
      scope: this,
      handler: this.onBtnProdPathDownClicked,
    });
    this.gridProdPath = Ext.create("Ext.grid.Panel", {
      flex: 1,
      title: T.label.productProcessPath,
      tbar: {
        xtype: "toolbar",
        items: [this.btnProdPathUp, this.btnProdPathDown],
      },
      columns: [
        {
          text: T.label.pathCode,
          dataIndex: "pathCode",
        },
        {
          text: T.label.pathDesc,
          dataIndex: "pathDesc",
          width: 200,
        },
      ],
      store: Ext.create("Ext.data.JsonStore", {
        fields: ["matCode", "matVer", "pathCode", "pathDesc"],
        proxy: {
          type: "zbusrpc",
          module: "SbasViewProductProcessPathService",
          reader: {
            root: "pathList",
          },
        },
      }),
      listeners: {
        scope: this,
        itemdblclick: this.onGridProdPathItemDblClick,
        itemclick: this.onGridProdPathItemClick,
      },
    });

    this.gridOpers = Ext.create("Ext.grid.Panel", {
      height: 200,
      title: T.label.operation,
      columns: [
        {
          text: T.label.pathCode,
          dataIndex: "pathCode",
          width: 100,
        },
        {
          text: T.label.sequence,
          dataIndex: "seqNum",
          width: 40,
        },
        {
          text: T.label.operation,
          dataIndex: "oper",
          width: 80,
        },
        {
          text: T.label.operationDesc,
          dataIndex: "operDesc",
          width: 150,
        },
      ],
      store: Ext.create("Ext.data.JsonStore", {
        fields: ["pathCode", "seqNum", "oper", "operDesc"],
        proxy: {
          type: "zbusrpc",
          module: "SmfrViewProcessPathOperationService",
          reader: {
            root: "operList",
          },
        },
      }),
    });

    //工艺路线面板
    this.pelPath = Ext.create("Ext.panel.Panel", {
      title: T.label.productProcessPath,
      layout: {
        type: "hbox",
        align: "stretch",
      },
      items: [
        this.pelAllPath,
        {
          flex: 1,
          layout: {
            type: "vbox",
            align: "stretch",
          },
          items: [this.gridProdPath, this.gridOpers],
        },
      ],
    });

    //右侧表单
    //查询
    this.txtSearchProdCode = Ext.create("Ext.form.field.Text", {
      fieldLabel: T.label.prodCode,
      labelWidth: 80,
      anchor: "98%",
    });
    this.frmSearch = Ext.create("Ext.form.Panel", {
      title: T.label.search,
      items: [this.txtSearchProdCode],
    });

    //操作
    this.txtProdCode = Ext.create("Ext.form.field.Text", {
      fieldLabel: T.label.prodCode,
      maxLength: 30,
      allowBlank: false,
      name: "matCode",
      labelWidth: 80,
      anchor: "98%",
    });
    this.txtProdVer = Ext.create("Ext.form.field.Text", {
      fieldLabel: T.label.ver,
      maxLength: 5,
      allowBlank: false,
      name: "matVer",
      value: "1",
      labelWidth: 80,
      anchor: "98%",
    });
    this.txtProdDesc = Ext.create("Ext.form.field.Text", {
      fieldLabel: T.label.prodDesc,
      maxLength: 100,
      name: "matDesc",
      labelWidth: 80,
      anchor: "98%",
    });
    this.txtProdSpec = Ext.create("Ext.form.field.Text", {
      fieldLabel: T.label.spec,
      maxLength: 50,
      name: "matSpec",
      labelWidth: 80,
      anchor: "98%",
    });
    this.cmbProdType = Ext.create("Ext.form.field.ComboBox", {
      fieldLabel: T.label.type,
      labelWidth: 80,
      anchor: "98%",
      editable: false,
      name: "matType",
      displayField: "data1",
      valueField: "key1",
      store: Ext.create("Ext.data.JsonStore", {
        autoLoad: true,
        fields: [
          {
            type: "string",
            name: "key1",
          },
          "data1",
        ],
        proxy: {
          type: "zbusrpc",
          module: "BasViewCommonDataSetupDetailService",
          extraParams: {
            groupName: "MATERIAL_TYPE",
          },
          reader: {
            type: "json",
            root: "cdsDtlList",
          },
        },
      }),
    });
    this.cmbProdUnit = Ext.create("Ext.form.field.ComboBox", {
      fieldLabel: T.label.unit,
      labelWidth: 80,
      anchor: "98%",
      editable: false,
      name: "unit",
      displayField: "data1",
      valueField: "key1",
      store: Ext.create("Ext.data.JsonStore", {
        autoLoad: true,
        fields: ["key1", "data1"],
        proxy: {
          type: "zbusrpc",
          module: "BasViewCommonDataSetupDetailService",
          extraParams: {
            groupName: "MATERIAL_UNIT",
          },
          reader: {
            type: "json",
            root: "cdsDtlList",
          },
        },
      }),
    });
    this.numProdQty = Ext.create("Ext.form.field.Number", {
      fieldLabel: T.label.qty,
      labelWidth: 80,
      anchor: "98%",
      minValue: 0,
      value: 1,
      name: "qty",
    });
    this.frmOperate = Ext.create("Ext.form.Panel", {
      title: T.label.operate,
      flex: 1,
      items: [
        this.txtProdCode,
        this.txtProdVer,
        this.txtProdDesc,
        this.txtProdSpec,
        this.cmbProdType,
        this.numProdQty,
        this.cmbProdUnit,
      ],
    });
    this.btnSave = Ext.create("Ext.button.Button", {
      text: T.label.save,
      itemId: "btnSave",
      iconCls: "icon-disk",
      scope: this,
      handler: this.onBtnSaveClicked,
    });
    this.btnDelete = Ext.create("Ext.button.Button", {
      text: T.label.del,
      itemId: "btnDelete",
      iconCls: "icon-delete",
      scope: this,
      handler: this.onBtnDeleteClicked,
    });
    this.btnSearch = Ext.create("Ext.button.Button", {
      text: T.label.search,
      itemId: "btnSearch",
      iconCls: "icon-find",
      scope: this,
      handler: this.onBtnSearchClicked,
    });
    this.btnReset = Ext.create("Ext.button.Button", {
      text: T.label.reset,
      itemId: "btnReset",
      iconCls: "icon-clear",
      scope: this,
      handler: this.onBtnResetClicked,
    });
    this.pelRight = Ext.create("Ext.panel.Panel", {
      width: 260,
      layout: {
        type: "vbox",
        align: "stretch",
      },
      items: [this.frmSearch, this.frmOperate],
      bbar: {
        xtype: "toolbar",
        items: [this.btnSave, this.btnDelete, this.btnSearch, this.btnReset],
      },
    });

    this.tabProd = Ext.create("Ext.tab.Panel", {
      flex: 1,
      items: [this.pelProdList, this.pelPath],
    });

    this.items = [this.tabProd, this.pelRight];

    this.callParent();
  },
  onTxtFilterPathSpecialKey: function (field, e) {
    //工艺路线过滤
    if (e.getKey() == e.ENTER) {
      this.gridAllPath
        .getStore()
        .getProxy()
        .setExtraParam("pathCode", field.getValue());
      this.gridAllPath.getStore().loadPage(1);
    }
  },
  onBtnProdPathUpClicked: function () {
    //产品工艺路线上调
    var rcds = this.gridProdPath.getSelectionModel().getSelection();
    if (Ext.isEmpty(rcds)) {
      return;
    }

    var store = this.gridProdPath.getStore();
    var index = store.indexOf(rcds[0]);
    if (index > 0) {
      var nRcd = rcds[0].copy();
      nRcd = store.insert(index - 1, nRcd);
      store.remove(rcds[0]);
      this.gridProdPath.getSelectionModel().select(nRcd, false, true);
    }
  },
  onBtnProdPathDownClicked: function () {
    //产品工艺路线下调
    var rcds = this.gridProdPath.getSelectionModel().getSelection();
    if (Ext.isEmpty(rcds)) {
      return;
    }

    var store = this.gridProdPath.getStore();
    var index = store.indexOf(rcds[0]);
    if (index < store.getCount() - 1) {
      var nRcd = rcds[0].copy();
      nRcd = store.insert(index + 2, nRcd);
      store.remove(rcds[0]);
      this.gridProdPath.getSelectionModel().select(nRcd, false, true);
    }
  },
  onBtnSaveClicked: function (btn) {
    if (!this.frmOperate.getForm().isValid()) {
      return;
    }

    var me = this;
    Ext.Msg.show({
      title: T.label.save,
      msg: T.msg.are_you_sure_to_save_data,
      icon: Ext.Msg.QUESTION,
      buttons: Ext.Msg.YESNO,
      fn: function (btnId) {
        if (btnId == "yes") {
          btn.disable();

          var vParms = me.frmOperate.getValues();
          if (me.gridProdPath.getStore().getCount() > 0) {
            vParms.pathList = [];
            me.gridProdPath.getStore().each(function (rcd) {
              vParms.pathList.push({
                pathCode: rcd.get("pathCode"),
              });
            });
          }

          YX.ZbusRpc.request({
            module: "SbasSaveProductService",
            params: vParms,
            showSuccess: true,
            callback: function () {
              btn.enable();
            },
            success: function () {
              me.frmOperate.getForm().reset();
              me.gridProd.getStore().loadPage(1);
            },
          });
        }
      },
    });
  },
  onBtnDeleteClicked: function (btn) {
    var rcds = this.gridProd.getSelectionModel().getSelection();
    if (Ext.isEmpty(rcds)) {
      return;
    }

    var me = this;
    Ext.Msg.show({
      title: T.label.save,
      msg: T.msg.are_you_sure_to_delete_data,
      icon: Ext.Msg.QUESTION,
      buttons: Ext.Msg.YESNO,
      fn: function (btnId) {
        if (btnId == "yes") {
          btn.disable();

          YX.ZbusRpc.request({
            module: "SbasUpdateProductStatusService",
            showSuccess: true,
            params: {
              matCode: rcds[0].get("matCode"),
              matVer: rcds[0].get("matVer"),
              status: "D",
            },
            callback: function () {
              btn.enable();
            },
            success: function () {
              me.frmOperate.getForm().reset();
              me.gridProd.getStore().loadPage(1);
            },
          });
        }
      },
    });
  },
  onBtnSearchClicked: function (btn) {
    var vMatCode = this.txtSearchProdCode.getValue();
    this.gridProd.getStore().load({
      params: {
        matCode: vMatCode,
      },
    });
  },
  onBtnResetClicked: function (btn) {
    this.frmOperate.getForm().reset();
  },
  onGridAllPathItemClick: function (grid, record) {
    this.gridOpers.getStore().load({
      params: {
        pathCode: record.get("pathCode"),
      },
    });
  },
  onGridAllPathItemDblClick: function (grid, record) {
    if (record.get("pathStatus") == "D") {
      Ext.Msg.show({
        title: T.label.error,
        msg: T.msg.operation_fail_when_data_is_invalid,
        icon: Ext.Msg.ERROR,
        buttons: Ext.Msg.OK,
      });

      return;
    }

    var index = this.gridProdPath
      .getStore()
      .findExact("pathCode", record.get("pathCode"));
    if (index >= 0) {
      this.gridProdPath
        .getSelectionModel()
        .select(this.gridProdPath.getStore().getAt(index), false, true);
    } else {
      this.gridProdPath.getStore().add(record.copy());
    }
  },
  onGridProdPathItemClick: function (grid, record) {
    this.gridOpers.getStore().load({
      params: {
        pathCode: record.get("pathCode"),
      },
    });
  },
  onGridProdPathItemDblClick: function (grid, record) {
    var rcds = this.gridProdPath.getSelectionModel().getSelection();
    if (!Ext.isEmpty(rcds)) {
      this.gridProdPath.getStore().remove(rcds);
    }
  },
  onGridProdSelect: function (sm, record) {
    this.frmOperate.loadRecord(record);
    this.gridProdPath.getStore().load({
      params: {
        matCode: record.get("matCode"),
        matVer: record.get("matVer"),
      },
    });
  },
});
